<%--
  Created by IntelliJ IDEA.
  User: SXLWTT
  Date: 2021/10/25
  Time: 10:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="/jquery-ui-1.10.4.custom/js/jquery-1.10.2.js"></script>
    <script src="/jquery-ui-1.10.4.custom/js/jquery-ui-1.10.4.custom.min.js"></script>
    <link rel="stylesheet" href="/jquery-ui-1.10.4.custom/css/base/jquery-ui-1.10.4.custom.min.css">

</head>
<body>
<form>
   <%-- <input type="text" name="aid"><br>--%>
    财务日期：<input type="date" name="adate"><br>
    原账号名称：<input type="text" id="yew">
       原账号：<input type="text" name="yid"><br>
    目标账号名称：<input type="text" id="mew">
       目标账号：<input type="text" name="mid"><br>
    科目编号：<input type="text" name="kid"><br>
    科目：<input type="text" name="kname"><br>
       摘要(备注)：<input type="text" name="kotes"><br>
       借方：<input type="text" name="debtor"><br>
       贷方：<input type="text" name="credit" ><br>
       余额：<input type="text" name="balance"readonly><br>
    <input type="button" value="添加" onclick="add()">
</form>
</body>
</html>
<script>

    $(function () {
        var cnamearray=new Array()
        var array=new Array()

        $.ajax({
            url:"listCompany.do",
            type:"post",
            dataType:"json",
            success:function (obj){
                for (var i in obj){
                    cnamearray.push(obj[i].cname)
                    array.push(obj[i])
                }

            }
        });
        $("#yew").autocomplete({
            source:cnamearray,delay: 300,
            minLength: 1,
            messages: {
                noResults:'',
                results: function() {
                }
            },
            select:function (event,ui) {
               var aaa= ui.item.value
                for (var i in array) {
                   if (aaa==array[i].cname){
                       $("[name='yid']").val(array[i].cid)
                   }
                }
            }
        });
        $("#mew").autocomplete({
            source:cnamearray,delay: 300,
            minLength: 1,
            messages: {
                noResults:'',
                results: function() {
                }
            },
            select:function (event,ui) {
                var aaa= ui.item.value
                for (var i in array) {
                    if (aaa==array[i].cname){
                        $("[name='mid']").val(array[i].cid)
                    }
                }
            }
        });
    })

    $("[name='credit']").change(function () {
        var kotes=$("[name='credit']").val()
        var debtor=$("[name='debtor']").val()
        $("[name='balance']").val(debtor-kotes)

    });
    function add() {
        if ($("[name='debtor']").val().length==0){
            alert("借方不能为空")
        }
        if ($("[name='credit']").val().length==0){
            alert("贷方不能为空")
        }


        $.ajax({
            url:"add.do",
            data:$("form").serialize(),
            type:"post",
            dataType:"json",
            success:function (obj) {
                if (obj>0){
                    alert("添加成功")
                    location="list.do"
                }
            }
        })
    }
</script>